package com.hongxinguoji.manager.younger.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hongxinguoji.manager.common.upload.FileUploadService;
import com.hongxinguoji.manager.common.util.EasyUIDataGridResult;
import com.hongxinguoji.manager.common.util.HXResult;
import com.hongxinguoji.manager.common.util.IDUtils;
import com.hongxinguoji.manager.younger.pojo.HxOutstandingEmployee;
import com.hongxinguoji.manager.younger.service.IOutstandingEmployeeService;

@Controller
@RequestMapping("/young")
public class OutstandingEmployeeController {

	@Autowired
	private IOutstandingEmployeeService outstandingEmployeeService;

	@Autowired
	private FileUploadService fileUploadService;

	@RequestMapping("/{path}")
	public String goPath(@PathVariable String path) {
		return "young/" + path;
	}

	@RequestMapping("/file/uploadHeaderImgFile")
	@ResponseBody
	public Map uploadHeaderImgFile(MultipartFile headerImgFile) {
		Map result = fileUploadService.uploadFile(headerImgFile);
		return result;
	}

	@RequestMapping("/file/uploadLiveImg")
	@ResponseBody
	public Map uploadlLiveImg(MultipartFile liveImgFile) {
		Map result = fileUploadService.uploadFile(liveImgFile);
		return result;
	}

	@RequestMapping("/save")
	@ResponseBody
	public HXResult youngAdd(HxOutstandingEmployee outstandingEmployee) {

		Long id = IDUtils.getArticleId();
		outstandingEmployee.setId(id);

		outstandingEmployee.setStatus(1);

		Date date = new Date();
		outstandingEmployee.setCreateDate(date);
		outstandingEmployee.setUpdateDate(date);

		try {
			outstandingEmployeeService.addYoung(outstandingEmployee);
			return HXResult.ok("增加优秀员工成功！");

		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "增加优秀员工失败！");
		}

	}

	@RequestMapping("/listcontent")
	@ResponseBody
	public EasyUIDataGridResult listcontent(Integer page, Integer rows) {

		List<HxOutstandingEmployee> list = outstandingEmployeeService.getYoungList();

		EasyUIDataGridResult result = new EasyUIDataGridResult();

		// 分页处理
		PageHelper.startPage(page, rows);

		PageInfo<HxOutstandingEmployee> pageInfo = new PageInfo<>(list);

		result.setTotal(pageInfo.getTotal());
		result.setRows(list);

		return result;

	}

	@RequestMapping("/lock")
	@ResponseBody
	public HXResult lockContent(String ids) {
		// 截取字符串
		String[] idStrings = ids.split(",");

		try {
			// 遍历ID，进行逻辑删除操作
			for (String id : idStrings) {
				String tID = id.trim();
				// 转换id类型
				Long i = Long.parseLong(tID);
				outstandingEmployeeService.lockYoung(i);

			}
			return HXResult.ok("锁定成功！");

		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "锁定失败！");
		}

	}

	@RequestMapping("/unlock")
	@ResponseBody
	public HXResult unlockContent(String ids) {
		// 截取字符串
		String[] idStrings = ids.split(",");

		try {
			// 遍历ID，进行逻辑删除操作
			for (String id : idStrings) {
				String tID = id.trim();
				// 转换id类型
				Long i = Long.parseLong(tID);
				outstandingEmployeeService.unlockYoung(i);

			}
			return HXResult.ok("解锁成功！");

		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "解锁失败！");
		}

	}

	@RequestMapping("/delete")
	@ResponseBody
	public HXResult deleteContent(String ids) {
		// 截取字符串
		String[] idStrings = ids.split(",");

		try {
			// 遍历ID，进行逻辑删除操作
			for (String id : idStrings) {
				String tID = id.trim();
				// 转换id类型
				Long i = Long.parseLong(tID);
				outstandingEmployeeService.deleteYoung(i);

			}
			return HXResult.ok("删除成功！");

		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.ok("删除失败！");
		}

	}

	@RequestMapping("/update")
	@ResponseBody
	public HXResult updateContent(HxOutstandingEmployee employee) {
		try {
			outstandingEmployeeService.updateContent(employee);
			return HXResult.ok("更新数据成功！");

		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "更新数据失败！");
		}

	}

}
